Imports BlazeApps.Library.Resources
Imports BlazeApps.Library.Options
Imports System.Text

Partial Class Usercontrols_Gallery_GalleryViewer
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "



#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        SetPageTitle("Gallery Viewer")
        SetPageState()

        If Me.Page.IsPostBack = False Then
            SetViewed(GetGuidFromQueryString("id"), False)
            LoadOptions()
        End If

        Dim Resource As ResourceDetail = ResourceDetail.GetResourceDetail(GetGuidFromQueryString("id"))

        If InStr(Resource.contenttype, "image/") Then
            Me.uxImageButton.ImageUrl = String.Format("~/default.aspx?pid=res_getresource&id={0}", GetGuidFromQueryString("id"))
            Me.uxImageButton.Width = New Unit(440, UnitType.Pixel)
            Me.uxImageButton.Height = New Unit(360, UnitType.Pixel)
            Me.uxImageButton.AlternateText = "Click for full picture"

            Me.uxImageButton.Visible = True
            Me.uxVideoPanel.Visible = False

            Me.uxDescriptionLabel.Text = Resource.resourcedesc
            Me.uxTitleLiteral.Text = StrConv(Resource.resourcetitle, vbProperCase)

        ElseIf InStr(Resource.contenttype, "video/") Then

            Me.uxVideoImage.ImageUrl = String.Format("~/default.aspx?pid=res_thumbnail&resourceid={0}", GetGuidFromQueryString("id"))
            Me.uxVideoImage.Width = New Unit(ThumbWidth + 100, UnitType.Pixel)
            Me.uxVideoImage.Height = New Unit(ThumbHeight + 100, UnitType.Pixel)

            Me.uxVideoHyperlink.Text = Resources.Resource.PlayVideo.ToString
            Me.uxVideoHyperlink.NavigateUrl = String.Format("~/default.aspx?pid=res_getresource&id={0}", Request.QueryString("id"))

            Me.uxImageButton.Visible = False
            Me.uxVideoPanel.Visible = True

            Me.uxDescriptionLabel.Text = Resource.resourcedesc
            Me.uxTitleLiteral.Text = StrConv(Resource.resourcetitle, vbProperCase)

        ElseIf InStr(Resource.resourcename, ".flv") Then

            Dim SB As New StringBuilder
            SB.Append("<object type=""application/x-shockwave-flash""")
            SB.Append(String.Format("width=""440"" height=""360"" wmode=""transparent"" data=""flvplayer.swf?file=default.aspx?pid=res_flash/{0}&autoStart=true"">", GetGuidFromQueryString("id")))
            SB.Append(String.Format("<param name=""movie"" value=""flvplayer.swf?file=default.aspx?pid=res_flash/{0}&autoStart=true""/>", GetGuidFromQueryString("id")))
            SB.Append("<param name=""wmode"" value=""transparent""/>")
            SB.Append("</object>")

            Me.uxLiteral.Text = SB.ToString

            Me.uxImageButton.Visible = False
            Me.uxVideoPanel.Visible = False

            Me.uxDescriptionLabel.Text = Resource.resourcedesc
            Me.uxTitleLiteral.Text = StrConv(Resource.resourcetitle, vbProperCase)

        ElseIf InStr(Resource.resourcename, ".swf") Then

            Dim SB As New StringBuilder
            SB.Append("<object classid=""clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"" codebase=""http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0"" id=""Test"" width=""440"" height=""366"">")
            SB.Append(String.Format("<param name=""movie"" value=""default.aspx?pid=res_flash/{0}""/>", GetGuidFromQueryString("id")))
            SB.Append("<param name=""quality"" value=""high"" />")
            SB.Append(String.Format("<embed src=""default.aspx?pid=res_flash/{0}"" FlashVars=""init=yes&check=true"" quality=""high"" NAME=""Test"" TYPE=""application/x-shockwave-flash"" pluginspage=""http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash type=application/x-shockwave-flash"" width=""440"" height=""366"" />", GetGuidFromQueryString("id")))
            SB.Append("</object>")

            Me.uxLiteral.Text = SB.ToString

            Me.uxImageButton.Visible = False
            Me.uxVideoPanel.Visible = False

            Me.uxDescriptionLabel.Text = Resource.resourcedesc
            Me.uxTitleLiteral.Text = StrConv(Resource.resourcetitle, vbProperCase)

        Else
            With Response
                .Clear()
                .ContentType = Resource.contenttype
                .AddHeader("Content-Disposition", "filename=" & Resource.resourcename)
                .BinaryWrite(Resource.resource)
                .End()
            End With
        End If

    End Sub

#End Region

#Region " Save Data "



#End Region

#Region " Load Data "

    Private Sub LoadOptions()
        Dim Items As OptionDetail = OptionDetail.GetOptions(GetGuidFromQueryString("id"))

        'Hide/Show Panels based on page settings
        Me.uxFavoritesPanel.Visible = (Items.AllowSubscriptions And EnableFavorites)
        Me.uxRaingPanel.Visible = (Items.AllowRanking And EnableRating)
        Me.uxCommentsPanel.Visible = (Items.AllowComments And EnableComments)
        Me.uxTagsPanel.Visible = (Items.AllowTags And EnableTags)
        Me.uxToolsPanel.Visible = (Items.AllowInfoTools And EnableTools)

        'Enable/Disable Panels based on page settings
        Me.uxFavoritesPanel.Enabled = (Items.AllowSubscriptions And EnableFavorites)
        Me.uxRaingPanel.Enabled = (Items.AllowRanking And EnableRating)
        Me.uxCommentsPanel.Enabled = (Items.AllowComments And EnableComments)
        Me.uxTagsPanel.Enabled = (Items.AllowTags And EnableTags)
        Me.uxToolsPanel.Enabled = (Items.AllowInfoTools And EnableTools)

        If GetBooleanFromQueryString("printme") Then
            Me.uxFavoritesPanel.Visible = False
            Me.uxRaingPanel.Visible = False
            Me.uxCommentsPanel.Visible = False
            Me.uxTagsPanel.Visible = False
            Me.uxToolsPanel.Visible = False

            Me.uxFavoritesPanel.Enabled = False
            Me.uxRaingPanel.Enabled = False
            Me.uxCommentsPanel.Enabled = False
            Me.uxTagsPanel.Enabled = False
            Me.uxToolsPanel.Enabled = False
        End If

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub ShowFullPicture(ByVal sender As Object, ByVal e As CommandEventArgs)

        ScriptManager.RegisterStartupScript(uxImageButton, Me.GetType(), "picture", "fPopUpWin('" & String.Format("default.aspx?pid=res_getresource&id={0}", GetGuidFromQueryString("id")) & "','picture',0 , 1, 1,790,500, 0);", True)

    End Sub

#End Region

#Region " Helpers and Methods "

    Private Sub SetPageState()

        If GetBooleanFromQueryString("popup") = True Then
            Me.uxCommentsList.Visible = False
            Me.uxCommentsList.HideData = True
            Me.uxTools.Visible = False
            Me.uxTools.HideData = True
            Me.uxRating.Visible = False
            Me.uxRating.HideData = True
            Me.uxFavorites.Visible = False
            Me.uxFavorites.HideData = True
            Me.uxPageTags.Visible = False
            Me.uxPageTags.HideData = True
        End If

    End Sub

#End Region

End Class
